<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 演示控制器
 */
namespace Addon\ResearchForm\Controller;
use Addon\ResearchForm\Controller\CommonController;

/**
 * 演示控制器 
 */
class IndexController extends CommonController{
	
	/**
	 * 演示操作 
	 */
	public function Index(){
    
		$m_research_form = M('research_form');
		
		$m_research_conf = M('research_conf');
		
		$input_type_map['type'] = 3;
		$input_type_map['status'] = 1;
		$input_type_list = $m_research_conf->where($input_type_map)->order('value asc')->select();
		
		$input_type = I('param.input_type');
		
		$input_type = $input_type?$input_type:1;
		foreach($input_type_list as $ti=>$tk){
			
			if($input_type_list[$ti]['value']==$input_type){
				
				$input_type_list[$ti]['class'] = 'primary';
				
			}else{
				
				$input_type_list[$ti]['class'] = 'default';
			}
			
			
		}
		
		$this->assign('list',$list);
		$this->assign('input_type_list',$input_type_list);
		$this->assign('input_type',$input_type);
		
		$id = I('param.id');
		$this->assign('id',$id);
		
		$research_data_map['id'] = $id;
	
		$data = $m_research_form->where($research_data_map)->find();
		
		$this->assign('data',$data);
		
		$research_map['form_id'] = $id;
		$research_map['type'] = 2;
		$research_map['status'] = 1;
		$list = $m_research_form->where($research_map)->order('sort desc')->select();
		
		$this->assign('list',$list);
		
		$file_map['form_id'] = $id;
		$file_map['type'] = 2;
		$file_map['input_type'] = 6;
		$file_map['status'] = 1;
		$file_list = $m_research_form->where($file_map)->order('sort desc')->select();
		
		$this->assign('file_list',$file_list);
		//插件专用视图输出
		$this->view();
    }
	
	
	/**
	 * 演示操作 
	 */
	public function Create(){
    
		$m_research_form = M('research_form');
		$m_research_info = M('research_info');
		
		$data['form_id'] = I('param.form_id');
		$data['field_id'] = 0;
		$data['pid'] = 0;
		$data['time'] = time();
		$data['ip'] = get_client_ip();
		$data['status'] = 1;
		
		$pid = $m_research_info->add($data);
		
		$info = I();
		
		
		unset($info['form_id']);
		
		foreach($info as $i=>$k){
			
			//判断是多选么？
			if(is_array($info[$i])){
				
				$field_list = $info[$i];
				
				foreach($field_list as $fi=>$fk){
					
					if($fi==0){
						
						$field_info = $field_list[$fi];
						
					}else{
						
						$field_info = $field_info.'|'.$field_list[$fi];
					}
					
				}
				
				$field_data['pid'] = $pid;
				$field_data['field_id'] = $i;
				$field_data['form_id'] = I('param.form_id');
				$field_data['value'] = $field_info;
				$field_data['time'] = time();
				$field_data['ip'] = get_client_ip();
				$field_data['status'] = 1;
				
				
		
				$m_research_info->add($field_data);
				
			}else{
				
				$field_data['pid'] = $pid;
				$field_data['field_id'] = $i;
				$field_data['form_id'] = I('param.form_id');
				$field_data['value'] = $info[$i];
				$field_data['time'] = time();
				$field_data['ip'] = get_client_ip();
				$field_data['status'] = 1;
				
				
				$m_research_info->add($field_data);	
			}
			
		}
		
		$this->success('提交成功');
	
		
    }
	
}